//获取应用实例
var api = require('../../utils/api.js');
var config = require('../../utils/config.js');
var lang = require('../../utils/lang.js');
var app = getApp();

Page({
    data: {
       s: {
         NowPage: 1,
         PageSize: 20,
         HasNextPage: 0,
         IsFavorite: 1,
         Info: [],

         LoadDisplay: 'none',
         IsBottom: false,
         ToEnd: '',
         NoInformation: '',
         onSearch: false,
         CurrencySymbol:''
       },

       windowHeight: 0,   //窗口高度
       ScrollTop: 0,
       onBottoming: false,
       InfoID:'',
       modalHidden: true,
       ImagesHeight: [],
    },

    onLoad: function () {
      var that = this;
      var CurrencySymbol = wx.getStorageSync('CurrencySymbol');
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            windowHeight: res.windowHeight
          })
        }
      });
      that.setData({
        's.PageSize': config.Common.CommonModelPageSize,
        's.CurrencySymbol': CurrencySymbol,
        's.ToEnd': lang.get("ToEnd"),
        's.NoInformation': lang.get("NoInformation"),
      });
      api.getFavorite().then(function (res) {
        if (res.Status == 1) {
          that.setData({
            's.Info': res.Data,
            's.HasNextPage': res.HasNextPage,
          })
        }
        if (res.Data == false) {
          that.setData({
            's.onSearch': true,
            's.IsBottom': false
          });
        }
      });
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: app.globalData.XcxThemeColor
      });
    },

    /**
    * 上拉刷新
    */
    onBottom: function (e) {
      var that = this;
      if (that.data.onBottoming) {   //防止出现重复上拉
        return;
      } else {
        that.setData({
          onBottoming: true,
        })
      }
      if (that.data.s.HasNextPage == 1) {
        that.setData({
          's.LoadDisplay': 'block',
          's.NowPage': that.data.s.NowPage + 1,
        })
        var params = {
          NowPage: that.data.s.NowPage,
          PageSize: that.data.s.PageSize,
        }
        api.getFavorite(params).then(function (res) {
          that.setData({
            's.Info': that.data.s.Info.concat(res.Data),
            's.HasNextPage': res.HasNextPage,
            's.LoadDisplay': 'none',
          })
          if (that.data.s.HasNextPage == 0) {
            that.setData({
              's.LoadDisplay': 'none',
              's.IsBottom': true
            })
          }
        }).then(function () {
          that.setData({
            onBottoming: false,
            's.onSearch': false,
          })
        })
      } else {
        that.setData({
          's.IsBottom': true
        });
      }
    },
    
    /**
     * 转发
     */
    onShareAppMessage: function (res) {
      return {
        path: '/pages/favorite/favorite',
      }
    },

    delFavorite: function (id) {
      var that = this;
      var params = {
        InfoID: id,
        IsAdd: 0
      }
      api.addFavorite(params).then(function (res) {
        var info = that.data.s.Info;
         for (var c in info) {
           if (info[c].InfoID == id) {                 
             info.splice(c, 1);
           }
         }
         that.setData({
           's.Info': info
         })
         if (info[0] == null) {
           that.setData({
             's.onSearch': true,
           });
         }
         wx.showToast({
           title: res.Message,
           icon: 'success',
           duration: 2000
         })
      });
    },
    
    /**
     * 显示提示
     */
    modalTap: function (e) {
      var that = this;
      var InfoID = e.target.dataset.id;
      that.setData({
        modalHidden: false,
        InfoID: InfoID
      })
    },

    /**
     * 隐藏并确定提示
     */
    modalChange: function (e) {
      var that = this;
      var id = that.data.InfoID;
      that.delFavorite(id);
      that.setData({
        modalHidden: true
      })
    },

    /**
     * 取消提示
     */
    modalCancel: function (e) {
      var that = this;
      that.setData({
        modalHidden: true
      })
    },
    
    /**
     * 图片自适应
     */
    bindImageLoad: function (e) {
      var that = this;
      var index = parseInt(e.currentTarget.dataset.index);
      var ImagesHeight = that.data.ImagesHeight;
      ImagesHeight[index] = app.globalData.screenWidth / (e.detail.width / e.detail.height);
      that.setData({
        ImagesHeight: ImagesHeight
      });
    },
});
